/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aicue1.services;

import aicue1.beans.Customer;
import aicue1.soapfaults.CustomerAlreadyExistsFault;
import aicue1.soapfaults.UnknownCustomerFault;
import javax.ws.rs.WebApplicationException;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import javax.ws.rs.core.Response;

/**
 *
 * @author Tom
 */
public class CustomerManagementServiceSoapImpl implements CustomerManagementServiceSoap
{
    CustomerManagementServiceRest restService = JAXRSClientFactory.create("http://localhost:8085/customers", CustomerManagementServiceRest.class);

    public Customer getCustomer(String id) throws UnknownCustomerFault
    {
        Customer customer = null;
        
        try
        {
            customer = restService.getCustomer(id);
        }
        catch(WebApplicationException exception)
        {
            throw new UnknownCustomerFault("User doesn't exist.");
        }
        
        return customer;
    }

    public void updateCustomer(String id, Customer customer) throws UnknownCustomerFault
    {
        if (restService.updateCustomer(id, customer).getStatus() != Response.Status.OK.getStatusCode())
            throw new UnknownCustomerFault("User doesn't exist.");
    }

    public void addCustomer(Customer customer) throws CustomerAlreadyExistsFault
    {
        if (restService.addCustomer(customer).getStatus() != Response.Status.OK.getStatusCode())
            throw new CustomerAlreadyExistsFault("User already exists.");
    }

    public void deleteCustomer(String id) throws UnknownCustomerFault
    {
        if (restService.deleteCustomer(id).getStatus() != Response.Status.OK.getStatusCode())
            throw new UnknownCustomerFault("User doesn't exist.");
    }
}